#include "pageembroadcast.h"
#include "ui_pageembroadcast.h"

PageEmBroadCast::PageEmBroadCast(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageEmBroadCast)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    initWidget();
}

PageEmBroadCast::~PageEmBroadCast()
{
    delete ui;
}

void PageEmBroadCast::initWidget()
{
    int BroadcastNum = GlobalConfig::spacialMessageList.size();
    int page = (BroadcastNum%ui->TableBroadcast->rowCount()) ? (BroadcastNum/ui->TableBroadcast->rowCount()+1) : (BroadcastNum/ui->TableBroadcast->rowCount());
    ui->LableTotalPage->setText(QString().setNum(page));
    ui->LableCurrPage->setText(QString().setNum(1));
    ui->TableBroadcast->setFrameShape(QFrame::NoFrame);     //无边框
    ui->TableBroadcast->setShowGrid(false);  //没有网格
    ui->TableBroadcast->setColumnWidth(0, 100);
    ui->TableBroadcast->setColumnWidth(1, 50);

    for(int i=0; i<ui->TableBroadcast->rowCount() && i<BroadcastNum; ++i)
    {
        QTableWidgetItem *item = new QTableWidgetItem(QString().setNum(i+1));
        item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
        ui->TableBroadcast->setItem(i, 0, item);

        item = new QTableWidgetItem("");
        ui->TableBroadcast->setItem(i, 1, item);

        item = new QTableWidgetItem(GlobalConfig::spacialMessageList.at(i).name);
        item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        ui->TableBroadcast->setItem(i, 2, item);
    }
    connect(ui->TableBroadcast, SIGNAL(itemClicked(QTableWidgetItem*)), SLOT(slotItemClicked(QTableWidgetItem*)));
}

void PageEmBroadCast::updateTableWidget()
{
    ui->TableBroadcast->clearSelection();
    for(int i = 0; i <ui->TableBroadcast->rowCount(); i++)
    {
        int index =  i + (ui->LableCurrPage->text().toInt()-1) * ui->TableBroadcast->rowCount();
        if(index >= GlobalConfig::spacialMessageList.size())
            for(int j=0; j<ui->TableBroadcast->columnCount(); ++j)
                ui->TableBroadcast->item(i, j)->setText("");
        else
        {
            ui->TableBroadcast->item(i, 0)->setText(QString().setNum(index+1));
            ui->TableBroadcast->item(i, 2)->setText(GlobalConfig::spacialMessageList.at(index).name);
        }
    }
}

void PageEmBroadCast::slotItemClicked(QTableWidgetItem *)
{
    if(ui->TableBroadcast->selectedItems().isEmpty())
        return;
    int index =  ui->TableBroadcast->currentRow() + (ui->LableCurrPage->text().toInt()-1) * ui->TableBroadcast->rowCount();
    if(index>=0 && index<GlobalConfig::spacialMessageList.size())
    {
        setIndexCast(index);
    }
}

void PageEmBroadCast::setIndexCast(int index)
{
    indexcast = index;
}

void PageEmBroadCast::slotTrigTimer()
{
    emit signal_EmBroadCastOnceValid(false);
}

void PageEmBroadCast::on_ButtonLastPage_clicked()
{
    int curpage = ui->LableCurrPage->text().toInt();
    if(curpage > 1)
    {
        ui->LableCurrPage->setText(QString().setNum(--curpage));
        updateTableWidget();
    }
}

void PageEmBroadCast::on_ButtonNextPage_clicked()
{
    int curpage = ui->LableCurrPage->text().toInt();
    int totalpage = ui->LableTotalPage->text().toInt();
    if(curpage < totalpage)
    {
        ui->LableCurrPage->setText(QString().setNum(++curpage));
        updateTableWidget();
    }
}

void PageEmBroadCast::on_ButtonSingle_clicked()
{
    emit signal_EmBroadCastOnceValid(true);
    QTimer::singleShot(3000, this, SLOT(slotTrigTimer()));
}

void PageEmBroadCast::on_ButtonLoop_clicked(bool checked)
{
    emit signal_EmBroadCastLoopValid(checked);
}

void PageEmBroadCast::syncBroadcastId(uint syncid)
{
    int syncindex=-1;
    for(int i=0;i<GlobalConfig::spacialMessageList.size();++i)
    {
        if(GlobalConfig::spacialMessageList.at(i).id == syncid)
        {
            syncindex = i;
            break;
        }
    }
    if(syncindex == -1)
        return;
    ui->LableCurrPage->setText(QString().setNum(syncindex/ui->TableBroadcast->rowCount()+1));
    updateTableWidget();
    ui->TableBroadcast->setCurrentCell(syncindex%ui->TableBroadcast->rowCount(),QItemSelectionModel::Select);
    setIndexCast(syncindex);
}
